package com.example.Controller;

import com.example.baseObj.AjaxResult;
import com.example.entity.Category;
import com.example.entity.Goods;
import com.example.entity.Role;
import com.example.repository.CategoryRepository;
import com.example.repository.GoodsRepository;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.crossstore.HashMapChangeSet;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by babyant on 17/3/6.
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    private static Logger logger = LoggerFactory.getLogger(GoodsController.class);

    private static final Integer SIZE = 10;

    @Autowired
    private GoodsRepository goodsRepository;

    @Autowired
    private CategoryRepository categoryRepository;

    @RequestMapping("/index")
    public ModelAndView index(ModelMap map, HttpServletRequest req){

        logger.info("进行查询操作");
        //由于前后台分页差1 查询的时候 要-1  返回去的时候+1
        Integer page = req.getParameter("page")==null?0:(Integer.parseInt(req.getParameter("page"))-1);
        Integer size = SIZE;
        Pageable pages = new PageRequest(page==null?0:page,size,new Sort(Sort.Direction.ASC,"id"));




        Page<Goods> goodsPage = goodsRepository.findAll(pages);

        List<Goods> goodsList = goodsPage.getContent();
        Integer totals = goodsPage.getTotalPages();

        List<Category> categories = categoryRepository.findAll();

        Map<Long,String> cmaps = new HashMap<Long,String>();
        for (Category c : categories){
            cmaps.put(c.getId(),c.getName());
        }

        map.put("cmaps",cmaps);


        map.put("totals",totals);
        map.put("page",page+1);
        map.put("goodslist",goodsList);
        map.put("menu_code","maingoods");
        return new ModelAndView("goods/index");
    }

    @RequestMapping("/add")
    public ModelAndView create(ModelMap map){

        List<Category> categories = categoryRepository.findAll();

        map.put("categories",categories);

        logger.info("进行新增操作");
        return new ModelAndView("goods/add_");
    }

    @RequestMapping("/save")
    public String save(Goods goods){

        goodsRepository.save(goods);

        logger.info("添加/更新的订单 ： "+goods.toString());

        return  "1";
    }
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public String delete(@PathVariable Long id){

        Goods goods = goodsRepository.findOne(id);
        goodsRepository.delete(goods);
        logger.info("删除->id : "+id);

        return "1";
    }

    @RequestMapping(value = "/deleteAll",method = RequestMethod.POST)
    public String deleteAll(HttpServletRequest req){

        String ids = req.getParameter("ids");
        if(!StringUtils.isEmpty(ids)){
            String idList[] = ids.split("-");
            for (int i = 0; i < idList.length; i++) {
                Long id = Long.valueOf(idList[i]);

                goodsRepository.delete(id);
            }
            return "1";
        }else{
            return "0";
        }
    }


    @RequestMapping(value = "/edit/{id}")
    public ModelAndView edit(@PathVariable Long id,ModelMap map){

        Goods goods = goodsRepository.findOne(id);


        List<Category> categories = categoryRepository.findAll();

        List<Long> cids = new ArrayList<Long>();
        for(Category c : categories){
            cids.add(c.getId());
        }

        map.put("cids",cids);
        map.put("categories",categories);

        map.put("goods",goods);
        logger.info("编辑->goods : "+goods.toString());
        logger.info("编辑->cids : "+cids.toString());


        return new ModelAndView("goods/edit");
    }

    @RequestMapping("/upload")
    public ModelAndView upload(ModelMap map){


        return new ModelAndView("goods/upload");
    }

}
